import java.awt.image.BufferedImage;
import java.util.HashMap;


/*
 * A class written to provide animated-gif "support" lol...
 * Basically, it takes the given image and decomposes it into
 * a 'library' of pictures which can be called, etc.
 * 
 */

public class AnimImage {

	BufferedImage img;
	HashMap< Integer, BufferedImage> imgMap;
	boolean loops = false;
	int currentFrame = 0;
	int frameWidth, frameHeight;
	int halfWidth, halfHeight;
	double samplerate;
	
//	.5 *(cos( 2*pi*x / 300 ) + 1 )
	public AnimImage( BufferedImage img, int frameWidth, int framerate){
		this.img = img;
		
		this.frameWidth = frameWidth;
		this.frameHeight = img.getHeight();
		halfWidth = (int)(frameWidth/2.0);
		halfHeight = (int)(frameHeight/2.0);
		
		samplerate = Math.round((double)60 / (double)framerate);
		
		imgMap = new HashMap<Integer, BufferedImage>();

		double j = 0;
		for(int i = 0; j<img.getWidth()/frameWidth; i++,j+=samplerate ){
			imgMap.put(i, img.getSubimage((int)Math.round(j)*frameWidth,0, frameWidth, img.getHeight()));			
		}
		
		
		
		
	}
	public boolean loopOnFinish(){
		return loops;
	}
	public void setCurrentFrame(int currentFrame){
		this.currentFrame = currentFrame;
	}
	public BufferedImage getNextFrame(){
//		System.out.print("frame was "+currentFrame);
		if(currentFrame == imgMap.size()-1){
			if(loops)
				currentFrame = 0;
			return imgMap.get(currentFrame);

		}else{
			return imgMap.get(currentFrame++);
		}
	}

	public BufferedImage getFrame(int t){
		if(loops){
			return imgMap.get(t%imgMap.size());	
		}else
			return imgMap.get(currentFrame);
	}
	public int getCurrentFrame(){
		return currentFrame;
	}
	public int getNumFrames(){
		return imgMap.size();
	}
	public int getFrameWidth(){
		return frameWidth;
	}
	public int getFrameHeight(){
		return frameHeight;
	}
	
	public int getFrameHalfWidth(){
		return halfWidth;
	}
	public int getFrameHalfHeight(){
		return halfHeight;
	}
	
	public void setLooping( boolean loops){
		this.loops = loops;
	}
	public void reset(){
		currentFrame = 0;
	}


}
